package uk.ac.cam.echo.app;

import java.util.LinkedList;
import java.util.List;
import uk.ac.cam.echo.ScheduleEvent;
import uk.ac.cam.echo.messages.LocationMessage;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

/**
 * @author Thom Sutcliffe, Tom Smith
 */

/*
 * TODO: Add documentation.
 */

public class EventsActivity extends SecondaryActivity
{

	private List<ScheduleEvent> eventList = new LinkedList<ScheduleEvent>();
	private List<LocationMessage> courtList = new LinkedList<LocationMessage>();

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.events);

		// get potential options from previous activity
		Bundle extras = getIntent().getExtras();
		final String courtRFID;

		final int spinnerSelected;

		// extract useful information from options (if present)
		if (extras != null)
		{
			courtRFID = extras.getString("courtRFID");

			spinnerSelected = extras.getInt("spinnerPos");

		}
		else
		{
			courtRFID = null;

			spinnerSelected = 3;
		}

		List<String> eventListString = new LinkedList<String>();
		List<String> courtListString = new LinkedList<String>();

		try
		{
			if (courtRFID == null || courtRFID.equals(""))
				eventList = DBInt.getInstance().getSchedule();
			else
				eventList = DBInt.getInstance().getSchedule(courtRFID);

			for (ScheduleEvent e : eventList)
			{
				eventListString.add(e.toString());
			}

			courtList = DBInt.getInstance().getLocations();

			courtList.add(new LocationMessage(null, "Overall"));

			for (LocationMessage l : courtList)
			{
				courtListString.add(l.toString());
			}
		}
		catch (Exception e)
		{
			showOKMessage("Error occured while connecting to the database.\n" + e.toString());
		}

		// UI components and data handling

		Button homeButton = (Button) findViewById(R.id.eventsHomeButton);
		Button mapButton = (Button) findViewById(R.id.eventsMapButton);
		final Spinner spinner = (Spinner) findViewById(R.id.eventsSpinner);
		final SpinnerAdapter spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, courtListString);
		final ListView listView = (ListView) findViewById(R.id.eventsView);

		final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, eventListString);

		// configuring components and data handlers
		spinner.setAdapter(spinnerAdapter);
		listView.setAdapter(adapter);
		spinner.setSelection(spinnerSelected);

		/*
		 * Defining operation with user input. Button operation, and
		 * functionality for selection of individual item.
		 */

		// Functionality for home button selected
		homeButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent i = new Intent(self, EchoActivity.class);
				self.startActivity(i);
				self.finish();
			}
		});

		// Functionality for event selected
		listView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				ScheduleEvent e = eventList.get(position);

				Intent i = new Intent(self, EventSpecificActivity.class);
				i.putExtra("eventName", e.getEventName());
				i.putExtra("eventDescription", e.getEventDescription());
				i.putExtra("eventStartTime", e.getStartTime());
				i.putExtra("eventEndTime", e.getEndTime());

				self.startActivity(i);
				self.finish();
			}
		});

		/*
		 * functionality for court selected.
		 */
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
		{

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
			{

				if (position != spinnerSelected)
				{
					LocationMessage l = courtList.get(position);

					Intent i = new Intent(self, EventsActivity.class);
					i.putExtra("courtRFID", l.getRFID());
					i.putExtra("spinnerPos", position);
					self.startActivity(i);
					self.finish();
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{
				spinner.setSelection(0);
			}
		});

		// Functionality for map button clicked
		mapButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent i = new Intent(self, MapActivity.class);
				i.putExtra("courtRFID", (courtRFID == null ? "" : courtRFID));
				i.putExtra("spinnerPos", spinnerSelected);
				self.startActivity(i);
				self.finish();
			}
		});
	}

}
